package com.zoffy.catdiarybackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.catdiarybackend.common.BusinessException;
import com.zoffy.catdiarybackend.common.IdRequest;
import com.zoffy.catdiarybackend.common.StateCode;
import com.zoffy.catdiarybackend.model.entity.Friendship;
import com.zoffy.catdiarybackend.model.enums.friendship.FriendshipStatusEnum;
import com.zoffy.catdiarybackend.model.vo.FriendVo;
import com.zoffy.catdiarybackend.model.vo.UserVo;
import com.zoffy.catdiarybackend.service.FriendshipService;
import com.zoffy.catdiarybackend.mapper.FriendshipMapper;
import com.zoffy.catdiarybackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86170
* @description 针对表【friendship】的数据库操作Service实现
* @createDate 2024-07-16 17:54:14
*/
@Service
public class FriendshipServiceImpl extends ServiceImpl<FriendshipMapper, Friendship>
    implements FriendshipService{
    @Resource
    @Lazy
    private UserService userService;
    @Resource
    FriendshipMapper friendshipMapper;
    @Override
    public boolean addFriend(HttpServletRequest request, IdRequest idRequest) {
        long user2Id = idRequest.getId();

        UserVo userVo = userService.getUserVo(request);
        if (userVo == null) {
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        UserVo friend = userService.getUserVo(user2Id);
        if(friend==null){
            throw new BusinessException(StateCode.ABNORMAL_OPERATION);
        }
        long user1Id = userVo.getId();
        if(friendshipMapper.addFriendIfExist(user1Id,user2Id)==0){
            friendshipMapper.addFriendIfNotExist(user1Id,user2Id);
        }


        return true;
    }

    @Override
    public boolean deleteFriend(HttpServletRequest request, IdRequest idRequest) {
        long user1Id = idRequest.getId();

        UserVo userVo = userService.getUserVo(request);
        if (userVo == null) {
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        long user2Id = userVo.getId();
        QueryWrapper<Friendship> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user1_id",user1Id);
        queryWrapper.eq("user2_id",user2Id);
        queryWrapper.or();
        queryWrapper.eq("user1_id",user2Id);
        queryWrapper.eq("user2_id",user1Id);
        Friendship friendship = this.getOne(queryWrapper);
        return this.removeById(friendship.getId());

    }

    @Override
    public List<FriendVo> selectFriend(HttpServletRequest request) {
        UserVo userVo = userService.getUserVo(request);
        if (userVo == null) {
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        List<FriendVo> friends = friendshipMapper.selectFriends(userVo.getId());
        return friends;
    }

    @Override
    public boolean checkFriend(long user1Id, long user2Id) {
        QueryWrapper<Friendship> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user1_id",user1Id);
        queryWrapper.eq("user2_id",user2Id);
        queryWrapper.or();
        queryWrapper.eq("user1_id",user2Id);
        queryWrapper.eq("user2_id",user1Id);
        Friendship friendship = this.getOne(queryWrapper);
        return friendship!=null;
    }

    @Override
    public List<FriendVo> selectFriendAddRequest(HttpServletRequest request) {
        UserVo userVo = userService.getUserVo(request);
        if(userVo==null){
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        List<FriendVo> friendVos = friendshipMapper.selectFriendAddRequest(userVo.getId());
        return friendVos;
    }

    @Override
    public List<FriendVo> selectMyFriendAddRequest(HttpServletRequest request) {
        UserVo userVo = userService.getUserVo(request);
        if(userVo==null){
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        List<FriendVo> friendVos = friendshipMapper.selectMyFriendAddRequest(userVo.getId());
        return friendVos;
    }

    @Override
    public boolean agreeFriendAddRequest(HttpServletRequest request, long userId) {
        UserVo userVo = userService.getUserVo(request);
        if(userVo==null){
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        QueryWrapper<Friendship> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user1_id",userId);
        queryWrapper.eq("user2_id",userVo.getId());
        Friendship friendship = this.getOne(queryWrapper);
        if(friendship==null){
            throw new BusinessException(StateCode.OPERATION_ERROR,"用户不存在");
        }
        friendship.setStatus(FriendshipStatusEnum.ESTABLISHED.getStatus());
        return this.updateById(friendship);
    }

    @Override
    public boolean disagreeFriendAddRequest(HttpServletRequest request, long userId) {
        UserVo userVo = userService.getUserVo(request);
        if(userVo==null){
            throw new BusinessException(StateCode.NOT_LOGIN_ERROR);
        }
        return friendshipMapper.disagreeFriendAddRequest(userVo.getId(),userId);
    }
}




